var data = {
    collectlist:[],
    shaopcartlist:[]
}

function reducer(state = data,action){
    //深拷贝
    var newState = JSON.parse(JSON.stringify(state));

    //判断action的类型
    if(action.type === 'COLLECT'){//收藏动作
        newState.collectlist.push(action.params)
    }else if(action.type === 'CANCELCOLLECT'){//取消收藏动作
         var index = newState.collectlist.findIndex((item)=>{
            return item.id == action.params.id;
         })
         if(index != -1 ){
            newState.collectlist.splice(index,1)
         }
    }else if(action.type === 'ADD'){//加入购物车动作
       newState.shaopcartlist.push(action.params)
    }else{
        return state
    }

    return newState;
}

export default reducer;